using B.S.XZYData.Api.Read.Extensions;
using B.S.XZYData.Infrastructure;

namespace B.S.XZYData.Api.Read
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args).Inject();
            var app = builder.Build();

            // 初始化数据库
            using (var scope = app.Services.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
                var initializer = new DatabaseInitializer(dbContext);
                
                try
                {
                    // 初始化数据库结构
                    initializer.Initialize();
                    
                    // 初始化基础数据
                    initializer.InitializeBaseData();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"数据库初始化失败：{ex.Message}");
                }
            }

            //跨域
            app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            // 鉴权
            app.UseAuthentication();

            app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}
